﻿using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Specialized;

// From: http://forums.silverlight.net/forums/t/103634.aspx
namespace SilverlightDebateJetPack
{
    public class CustomTabControl : TabControl
    {
        public CustomTabControl()
            : base()
        {
        }

        public DataTemplate TabHeaderItemTemplate
        {
            get { return (DataTemplate)GetValue(TabHeaderItemTemplateProperty); }
            set { SetValue(TabHeaderItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty TabHeaderItemTemplateProperty =
            DependencyProperty.Register("TabHeaderItemTemplate", typeof(DataTemplate), typeof(CustomTabControl), new PropertyMetadata(
                (sender, e) =>
                {
                    ((CustomTabControl)sender).InitTabs();
                }));

        public DataTemplate TabItemTemplate
        {
            get { return (DataTemplate)GetValue(TabItemTemplateProperty); }
            set { SetValue(TabItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty TabItemTemplateProperty =
            DependencyProperty.Register("TabItemTemplate", typeof(DataTemplate), typeof(CustomTabControl), new PropertyMetadata(
                (sender, e) =>
                {
                    ((CustomTabControl)sender).InitTabs();
                }));

        public IEnumerable MyItemsSource
        {
            get
            {
                return (IEnumerable)GetValue(MyItemsSourceProperty);
            }
            set
            {
                SetValue(MyItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty MyItemsSourceProperty =
            DependencyProperty.Register("MyItemsSource", typeof(IEnumerable), typeof(CustomTabControl), new PropertyMetadata(
                (sender, e) =>
                {
                    CustomTabControl control = (CustomTabControl)sender;
                    INotifyCollectionChanged incc = e.OldValue as INotifyCollectionChanged;
                    if (incc != null)
                    {
                        incc.CollectionChanged -= control.MyItemsSource_CollectionChanged;
                    }
                    control.InitTabs();

                    incc = e.NewValue as INotifyCollectionChanged;
                    if (incc != null)
                    {
                        incc.CollectionChanged += control.MyItemsSource_CollectionChanged;
                    }
                }));


        internal void InitTabs()
        {
            Items.Clear();
            if (MyItemsSource != null)
            {
                foreach (var item in MyItemsSource)
                {
                    var newitem = new TabItem();

                    if (TabItemTemplate != null)
                        newitem.Content = TabItemTemplate.LoadContent();

                    if (TabHeaderItemTemplate != null)
                        newitem.Header = TabHeaderItemTemplate.LoadContent();

                    newitem.DataContext = item;
                    Items.Add(newitem);
                }             
            }
        }

        void MyItemsSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                if (e.NewStartingIndex > -1)
                {
                    foreach (var item in e.NewItems)
                    {
                        var newitem = new TabItem();

                        if (TabItemTemplate != null)
                            newitem.Content = TabItemTemplate.LoadContent();

                        if (TabHeaderItemTemplate != null)
                            newitem.Header = TabHeaderItemTemplate.LoadContent();

                        newitem.DataContext = item;

                        Items.Add(newitem);
                    }
                }
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                if (e.OldStartingIndex > -1)
                {
                    Items.RemoveAt(e.OldStartingIndex);
                }
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
            {
                Items.RemoveAt(e.OldStartingIndex);

                var newitem = new TabItem();

                if (TabItemTemplate != null)
                    newitem.Content = TabItemTemplate.LoadContent();

                if (TabHeaderItemTemplate != null)
                    newitem.Header = TabHeaderItemTemplate.LoadContent();

                newitem.DataContext = e.NewItems[0];

                Items.Add(newitem);

                Items.Insert(e.NewStartingIndex, newitem);
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
            {
                InitTabs();
            }
        }
    }
}
